/* usrAppInit.c - stub application initialization routine */

/* Copyright 1984-1998 Wind River Systems, Inc. */

/*
modification history
--------------------
01a,02jun98,ms   written
*/

/*
DESCRIPTION
Initialize user application code.
*/ 

/******************************************************************************
*
* usrAppInit - initialize the users application
*/ 

#include <vxworks.h>
#include <configAll.h>
#include <sysSymTbl.h>
#include <stdio.h>

extern void bspDriverInit(void);
extern int sup_init(void);
extern int yaffs_start_up(void);

void usrAppInit (void)
    {
#ifdef	USER_APPL_INIT
	USER_APPL_INIT;		/* for backwards compatibility */
#endif

	bspDriverInit();
    yaffs_start_up();

#if 1
	taskSpawn("root", 10, 0,
   	          64000, (void*)sup_init,
   	          0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
#endif
    return;

    /* add application specific code here */
}

#define SYSBT_MAX_FUNC_WORDS	4096

LOCAL void sysbt_addr2sym(char *symName, UINT addr)
{
    extern SYMTAB_ID	sysSymTbl;
    UINT		symVal;
    SYM_TYPE		symType;
    int                 rc;

#if VX_VERSION==55 || VX_VERSION==62
    char                *symNameTmp;
    
    rc = symByValueFind(sysSymTbl,
		       (UINT) addr,
		       &symNameTmp, &symVal, &symType);
    if (rc != ERROR)
        strcpy(symName, symNameTmp);
#else
    rc = symFindByValue(sysSymTbl,
		       (UINT) addr,
		       symName, &symVal, &symType);
#endif
    if (rc == ERROR)
        sprintf(symName, "0x%x", addr);
    else if (symVal < addr)
	sprintf(symName + strlen(symName),
		" + 0x%x", addr - symVal);
    else if (symVal > addr)
	sprintf(symName + strlen(symName),
		" - 0x%x", symVal - addr);
}


void sysBacktracePpc(char *pfx, char *sfx, int direct)
{
    UINT		*sp, *pc, *fn_start, *fn_pro, *caller_sp, *ra;
    extern UINT		*vxSpGet(), *vxPcGet();		/* sysALib.s */
    int			limit, first = 1;
    char		buf[2048], *s;
    char		symName[MAX_SYS_SYM_LEN+1];
#if 0
    sp = vxSpGet();
    pc = vxPcGet();

    s = buf;

    strcpy(s, pfx);
    while (*s) s++;

    while (s < &buf[sizeof (buf) - 128]) {
	fn_start = pc;
	for (limit = 0; limit < SYSBT_MAX_FUNC_WORDS; limit++) {
	    /* mfspr r0, LR */
	    if (*fn_start-- == 0x7c0802a6)
		break;
	}
	if (limit == SYSBT_MAX_FUNC_WORDS ||
	    (*fn_start & 0xffff8000) != 0x94218000) /* stwu r1,x(r1); x<0 */
	    break;
	fn_pro = fn_start;
	for (limit = 0; limit < SYSBT_MAX_FUNC_WORDS; limit++)
	    if ((*++fn_pro & 0xffff8000) == 0x90010000)	/* stw r0,y(r1); y>0 */
		break;
	if (limit == SYSBT_MAX_FUNC_WORDS)
	    break;
	ra = (UINT *) sp[(*fn_pro & 0x0000ffff) / 4];
	caller_sp = (UINT *) sp[0];
	sysbt_addr2sym(symName, (UINT) pc);
	if (! first)
	    sprintf(s,
		    "FUNC = 0x%x, PC = 0x%x (%s), SP = 0x%x\n",
		    (UINT) fn_start, (UINT) pc, symName, (UINT) sp);
	while (*s) s++;
	if ((UINT) ra < 0x100 ||
	    (UINT) ra >= (UINT) sysPhysMemTop()) {
	    sprintf(s, "RA 0x%x out of range\n", (UINT) ra);
	    while (*s) s++;
	    break;
	}
	if ((UINT) caller_sp < 0x100 ||
	    (UINT) caller_sp >= (UINT) sysPhysMemTop()) {
	    sprintf(s, "Caller SP 0x%x out of range\n", (UINT) caller_sp);
	    while (*s) s++;
	    break;
	}
	pc = ra;
	sp = caller_sp;
	first = 0;
    }

    strcpy(s, sfx);

    if (direct)
	sysSerialPrintString(buf); 	/* Atomically print giant string */
    else {
	fputs(buf, stdout);		/* Regular print giant string */
	fflush(stdout);
    }
	#endif
}

int sysToMonitorExcMessage = 1;
int sysToMonitorBacktrace = 1;
int sysToMonitorReboot = 1;
void (*sysToMonitorHook)(void)=0;

#if 1

#define NFS_MAXPATH             255 
const int nfsMaxPath = NFS_MAXPATH + 1;

int sysSlotIdGet()
{
	return bspBoardGetSlotID();
}

int sysIsCFM()
{
    return 0;
}

void sysSerialPrintString(char *s )
{
	logMsg(s, 0,0,0, 0,0,0);
}



#endif

#ifndef INT_VEC_IRQ0
#define INT_VEC_IRQ0 0
#endif

int   sysVectorIRQ0 = INT_VEC_IRQ0;



int slot_id_get()
{
	return  bspBoardGetSlotID();
	}
	
	
int sysReboot()
{
	bspCpldResetSelfBoard();
}



/******************************************************************************
*
* usrStandaloneInit - initialize the built-in symbol table
*/ 

void SupUsrStandaloneInit (void)
    {
    int ix;

    sysSymTbl = symTblCreate (SYM_TBL_HASH_SIZE_LOG2, TRUE, memSysPartId);

    for (ix = 0; ix < standTblSize; ix++)       /* fill in from built in table*/
#if     ((CPU_FAMILY == ARM) && ARM_THUMB)
        thumbSymTblAdd (sysSymTbl, &(standTbl[ix]));
#else
        symTblAdd (sysSymTbl, &(standTbl[ix]));
#endif  /* CPU_FAMILY == ARM */
    }

